package com.itheima.web;

import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/login")
public class UserServlet extends BaseServlet {
    public void login(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException, ServletException {
        resp.setContentType("application/json;charset=utf-8");
        User user = new User();
        BeanUtils.populate(user,req.getParameterMap());
        Boolean flag = UserService.login(user);
        System.out.println(flag);
        if (flag) {
            HttpSession session = req.getSession();
            session.setAttribute("user",user);
        }
        resp.getWriter().print(flag);
    }
}
